package com.cnhis.cloudhealth.clinical.hosnurse.hoschangeresidentinformation.bo;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.hosnurse.hoschangeresidentinformation.dao.ChangeResidentInformationDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.DateUtils;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
@SuppressWarnings({"rawtypes","unchecked"})
@Service
public class ChangeResidentInformationBo {
  @Resource
  private ChangeResidentInformationDao changeResidentInformationDao;
  @Resource
  private GetNewId getNewId;
  public ModelVo execNurseChangedItem(Map map)throws Exception{
	   ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
	   String lMode = map.get("lMode").toString();
	   map.put("lDate", DateUtils.getDateString(new Date()));
	   if(checkParams(map)){
		   mo.setResult(StaticKeys.OH_FALSE);
		   mo.setResultMsg("病人已经预出院或出院");
		   return mo;
	   }
	   if("8".equals(map.get("lMode").toString())&&checkParams1(map)){
		   mo.setResult(StaticKeys.OH_FALSE);
		   mo.setResultMsg("当前住院医生编号和对应名称不匹配，请返回重新检索住院医生");
		   return mo;
	   }
	   Map map1 = getOfficeId(map);
	   map.putAll(map1);
	   updateBCQ1_VAE1_VBO1(map);
	   //11:调整主治医师,
	   if("11".equals(lMode)){
		    if(checkParams2(map)){
		    	changeResidentInformationDao.updateVAM31(map);
		    }else{
		    	changeResidentInformationDao.insertVAM31(map);
		    }
		   updateVAO11(map);
	   }
	   //12:调整主任医师,
	   if("12".equals(lMode)){
		   if(checkParams2(map)){
		    	changeResidentInformationDao.updateVAM27(map);
		    }else{
		    	changeResidentInformationDao.insertVAM27(map);
		    }
		   updateVAO12(map);
	   }
	   //15,调整一级质控员
	   if("15".equals(lMode)){
		   if(checkParams2(map)){
		    	changeResidentInformationDao.updateVAMA4(map);
		    }else{
		    	changeResidentInformationDao.insertVAMA4(map);
		    }
		   updateVAO15(map);
	   }
	   //16:调整二级质控员
	   if("16".equals(lMode)){
		   if(checkParams2(map)){
		    	changeResidentInformationDao.updateVAMA5(map);
		    }else{
		    	changeResidentInformationDao.insertVAMA5(map);
		    }
		   updateVAO16(map);
	   }
	   return mo;
  }
  
  private boolean checkParams(Map map)throws Exception{
	  return changeResidentInformationDao.checkParams(map)==0;
  }
  private boolean checkParams1(Map map)throws Exception{
	  return changeResidentInformationDao.checkParams1(map)==0;
  }
  private boolean checkParams2(Map map)throws Exception{
	  return changeResidentInformationDao.checkParams2(map)>0;
  }
  private Map getOfficeId(Map map)throws Exception{
	  return changeResidentInformationDao.getOfficeId(map);
  }
  private void updateBCQ1_VAE1_VBO1(Map map)throws Exception{
	  map.put("lID", getNewId.nextId());
	  changeResidentInformationDao.updateBCQ1_VAE1_VBO1(map);
  }
  private void updateVAO11(Map map)throws Exception{
	  map.put("lID", getNewId.nextId());
	  changeResidentInformationDao.updateVAO11(map);
  }
  private void updateVAO12(Map map)throws Exception{
	  map.put("lID", getNewId.nextId());
	  changeResidentInformationDao.updateVAO12(map);
  }
  private void updateVAO15(Map map)throws Exception{
	  map.put("lID", getNewId.nextId());
	  changeResidentInformationDao.updateVAO15(map);
  }
  private void updateVAO16(Map map)throws Exception{
	  map.put("lID", getNewId.nextId());
	  changeResidentInformationDao.updateVAO16(map);
  }
  
  public ModelVo execNurseVBM2BEP07edit(Map map)throws Exception{
	  ModelVo mo = new ModelVo(StaticKeys.OH_SUCCESS);
	  String ldate = DateUtils.getDateString(new Date());
	  map.put("ldate", ldate);
	  if(checkParams3(map)){
		  changeResidentInformationDao.updateVBM2_BEP07(map);
	  }else{
		  insertVBM2(map);
	  }
	  updateVAO1(map);
	  return mo;
  }
  
  private boolean checkParams3(Map map)throws Exception{
	  return changeResidentInformationDao.checkParams3(map)>0;
  }
  private void insertVBM2(Map map)throws Exception{
	  map.put("VBM01", getNewId.nextId());
	  changeResidentInformationDao.insertVBM2(map);
  }
  
  private void updateVAO1(Map map)throws Exception{
	  map.put("lid", getNewId.nextId());
	  changeResidentInformationDao.updateVAO1(map);
  }
  public ModelVo queryCareLevel()throws Exception{
	  ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=changeResidentInformationDao.queryCareLevel();
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
  }
  public ModelVo queryPatientCondition()throws Exception{
	  ModelVo mo = new ModelVo();
		List list =null;
		try {
			list=changeResidentInformationDao.queryPatientCondition();
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
  }
  
  public ModelVo queryInHospitalDoctor(int lType)throws Exception{
	  ModelVo mo = new ModelVo();
		List list =null;
		try {
			Map hs = new HashMap();
			hs.put("lType", lType);
			hs.put("Parav", changeResidentInformationDao.paramValue());
			list=changeResidentInformationDao.queryInHospitalDoctor(hs);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
  }
  public ModelVo dutyNurse(int lType)throws Exception{
	  ModelVo mo = new ModelVo();
		List list =null;
		try {
			HashMap hs = new HashMap();
			hs.put("lType", lType);
			hs.put("Parav", changeResidentInformationDao.paramValue());
			list=changeResidentInformationDao.dutyNurse(hs);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
  }
  public ModelVo queryAttendDoctor(int lType)throws Exception{
	  ModelVo mo = new ModelVo();
		List list =null;
		try {
			HashMap hs = new HashMap();
			hs.put("lType", lType);
			hs.put("Parav", changeResidentInformationDao.paramValue());
			list=changeResidentInformationDao.queryAttendDoctor(hs);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
  }
  public ModelVo queryChairManDoctor(int lType)throws Exception{
	  ModelVo mo = new ModelVo();
			List list =null;
			try {
				HashMap hs = new HashMap();
				hs.put("lType", lType);
				hs.put("Parav", changeResidentInformationDao.paramValue());
				list=changeResidentInformationDao.queryChairManDoctor(hs);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(list);
			return mo;
  }
  public ModelVo queryOneLevel(int lType)throws Exception{
	  ModelVo mo = new ModelVo();
		List list =null;
		try {
			HashMap hs = new HashMap();
			hs.put("lType", lType);
			hs.put("Parav", changeResidentInformationDao.paramValue());
			list=changeResidentInformationDao.queryOneLevel(hs);
			
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(list);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(list);
		return mo;
  }
	
	public ModelVo queryTwoLevel(int lType)throws Exception{
		  ModelVo mo = new ModelVo();
			List list =null;
			try {
				HashMap hs = new HashMap();
				hs.put("lType", lType);
				hs.put("Parav", changeResidentInformationDao.paramValue());
				list=changeResidentInformationDao.queryTwoLevel(hs);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(list);
			return mo;
	}
}
